package com.xuelangyun.form.common.security;

import javax.servlet.http.HttpServletRequest;

import com.xuelangyun.form.common.utils.HttpContextUtils;
import com.xuelangyun.form.common.utils.SpringContextUtils;
import com.xuelangyun.form.sys.entity.SysUserEntity;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.xuelangyun.form.oauth2.service.IOauthService;

/**
 * 认证公共类,获取当前用户等信息
 * @author weiqing.hk
 * @date 2024年6月7日17:07:16
 */
@Component
@Slf4j
public class AuthSecurityUtils {

	@Autowired
	private IOauthService service ;
 
	public SysUserEntity getCurrentUser() {

		  //获取请求token
    	HttpServletRequest hsr = HttpContextUtils.getHttpServletRequest();
		String token = HttpContextUtils.getRequestToken(hsr , "token") ;
		if(StringUtils.isBlank(token)) {
			log.info("token is empty.");
			return null ;
		}
		return service.queryByToken(token);
	}
	
	
	/**
	 * static 方法请求 当前用户 
	 * @return
	 */
	public static SysUserEntity getUser() {
		AuthSecurityUtils securityUtils = SpringContextUtils.getBean(AuthSecurityUtils.class);
		if(securityUtils != null) {
			return securityUtils.getCurrentUser();
		}
		return null ;
	}
}
